package com.hou.controller;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.hou.pojo.Place;
import com.hou.pojo.Result;
import com.hou.service.PlaceService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2021-09-23-7:37
 */
@RestController
@RequestMapping("place")
@CrossOrigin
public class PlaceController {

    @Autowired
    private PlaceService placeService;


    // 分页查询
    @GetMapping("list")
    public Map<String, Object> findByPage(Integer page, Integer rows, String provinceId) {
        // 空值初始化
        page = page == null ? 1 : page;
        rows = rows == null ? 5 : rows;
        Integer totalPages;
        HashMap<String, Object> map = new HashMap<>();
        // 一页数据
        List<Place> place = placeService.findByProvinceIdPage(page, rows, provinceId);
        // 所有数据
        Integer totals = placeService.findByProvinceIdCounts(provinceId);
        // 页数
        totalPages = totals % rows == 0 ? totals / rows : totals / rows + 1;
        // 放入map集合
        map.put("place", place);
        map.put("totals", totals);
        map.put("totalPages", totalPages);
        map.put("page", page);
        return map;
    }

    // 保存景点信息
    @PostMapping("save")
    public Result save(MultipartFile pic, Place place) throws IOException {

        Result result = new Result();

        try {
            String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "." + extension;
            // base64编码
            place.setPicpath(Base64Utils.encodeToString(pic.getBytes()));
//            // 文件上传
//            File file = new File("F:\\ProjectImageUpLoad");
//            pic.transferTo(new File(file,newFileName));

            placeService.save(place);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatue(false).setMsg("添加失败");
        }
        return result;
    }

    // 删除景点信息
    @GetMapping("delete")
    public Result delete(Integer id) {
        Result result = new Result();
        try {
            placeService.delete(id);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatue(false).setMsg("删除失败");
        }
        return result;
    }

    // 景点信息回显
    @GetMapping("getPlace")
    public Place getProvince(Integer id) {
        Place place = placeService.getPlaceById(id);
        return place;
    }

    @PostMapping("update")
    public Result updatePlace(Place place) {
        Result result = new Result();
        return result;
    }
}
